package com.talent.proxy.mysql.common.packets.s2c.resultset;

import java.nio.ByteBuffer;

import com.talent.aio.common.ChannelContext;
import com.talent.proxy.mysql.common.MysqlExt;
import com.talent.proxy.mysql.common.MysqlPacket;
import com.talent.proxy.mysql.common.MysqlReturn;
import com.talent.proxy.mysql.common.utils.BufferUtil;

/**
 * 
 * <pre><b>column count packet.</b></pre>
 * @author 
 * <pre>seaboat</pre>
 * <pre><b>email: </b>849586227@qq.com</pre>
 * <pre><b>blog: </b>http://blog.csdn.net/wangyangzhizhou</pre>
 * @version 1.0
 * @see http://dev.mysql.com/doc/internals/en/com-query-response.html#text-resultset
 */
public class ResultSetHeaderPacket extends MysqlPacket
{
	public int columnCount;
	public long extra;

	public boolean isCompletePacket()
	{
		return false;
	}
	
	public void decodeBody(ByteBuffer buffer, ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		int initposition = buffer.position();

		this.columnCount = (int) BufferUtil.readLength(buffer);

		int usedlength = buffer.position() - initposition;
		if (usedlength < bodySize)
		{
			extra = BufferUtil.readLength(buffer);
		}
	}

	@Override
	public ByteBuffer encode(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		int bodySize = bodySize();
		ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE + bodySize);
		BufferUtil.writeUB3(buffer, bodySize);
		buffer.put(seqId);
		BufferUtil.writeLength(buffer, columnCount);
		return buffer;
	}

	@Override
	public int bodySize()
	{
		int size = BufferUtil.getLength(columnCount);
		return size;
	}

	@Override
	protected String packetDesc()
	{
		return "MySQL Column Count Packet";
	}

	/** 
	 * @see com.talent.proxy.mysql.common.MysqlPacket#onReceived(com.talent.aio.common.ChannelContext)
	 * 
	 * @param channelContext
	 * @return
	 * @重写人: tanyaowu
	 * @重写时间: 2017年1月23日 下午4:59:22
	 * 
	 */
	@Override
	public MysqlReturn onReceived(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
